package com.yunhe.common.aspect;

import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.util.LogUtil;
import com.yunhe.common.util.StringUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(-5)
public class SystemMonitorLogAspect {

    @Autowired
    private LogUtil logUtil;

    @Pointcut("@annotation(com.yunhe.common.annotation.OperationRecord)")
    public void operationAspect(){}

    @AfterReturning(value = "operationAspect()", returning = "result")
    public void recordOperation(JoinPoint joinPoint, Object result) throws Throwable {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        OperationRecord operationRecord = method.getAnnotation(OperationRecord.class);
        String action = operationRecord.action();
        SystemConsts.ACTION actionType = operationRecord.actionType();
        String menu = operationRecord.menu();
        if (!StringUtil.isEmpty(action)) {
            logUtil.createActionLog(menu, action);
        } else {
            logUtil.createActionLog(menu, actionType);
        }
    }
}
